<template>
    <div class="singer-table">
        <div class="singer-container">
            <div class="handle-box">
                <el-input v-model="select_word" size='mini' placeholder="请输入歌手名" class="handle-input"/>
                <el-button type="primary" size="mini" @click="centerDialogVisible = true">添加歌手</el-button>
                <el-button type="danger" size="mini" @click="delAll">批量删除</el-button>
            </div>
        </div>
        <el-table size="mini" class="singertables" border :data="data" @selection-change="handleSelectionChange">
            <el-table-column type="selection"  width="40"></el-table-column>
            <el-table-column label="歌手图片" width="110" align="center">
                <template v-slot='scope'>
                    <div class="singer-image">
                        <img :src="getUrl(scope.row.pic)" alt="">
                    </div>
                    <el-upload 
                    :action="uploadUrl(scope.row.id)" 
                    :on-success="handleAvatorSuccess" 
                    :before-upload="beforeAvatorUpdate">
                        <el-button size="mini">更新图片</el-button>
                    </el-upload>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="歌手" width="120" align="center"></el-table-column>
            <el-table-column label="性别" width="50" align="center">
                <template v-slot="scope">
                    {{changeSex(scope.row.sex)}}
                </template>
            </el-table-column>
            <el-table-column prop="birth" label="生日" width="120" align="center">
                <template v-slot="scope">
                    {{scope.row.birth}}
                </template>
            </el-table-column>
            <el-table-column prop="location" label="地区" width="100" align="center"></el-table-column>
            <el-table-column label="简介" align="center">
                <template v-slot="scope">
                    <p style="height:100px;overflow:scroll">{{scope.row.introduction}}</p>
                </template>
            </el-table-column>
            <el-table-column width="120" label="歌曲管理" align="center">
                <template v-slot="scope">
                    <el-button size="mini" @click="songEdit(scope.row.id,scope.row.name)">歌曲管理</el-button>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="150">
                <template v-slot="scope">
                    <el-button size="mini"  type="primary" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini"  type="danger" @click="handleDelete(scope.row.id)">删除</el-button>

                </template>
            </el-table-column>
        </el-table>

        <div class="pagination">
            <el-pagination
                background
                layout="total,prev,pager,next"
                :current-page="currentPage"
                :page-size="pageSize"
                :total="singers.length"
                @current-change="handleCurrentChange"/>
        </div>

        <el-dialog title="添加歌手"  :visible.sync="centerDialogVisible" width="400px" center>
            <el-form :model="registerForm" ref="registerForm" label-width="80px">
                <el-form-item prop="name" label="歌手名" size="mini">
                    <el-input v-model="registerForm.name" placeholder="歌手名"></el-input>
                </el-form-item>
                <el-form-item prop="sex" label="性别" size="mini">
                    <el-radio-group v-model="registerForm.sex">
                        <el-radio :label="0">女</el-radio>
                        <el-radio :label="1">男</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="birth" label="日期" size="mini">
                    <el-date-picker v-model="registerForm.birth" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item prop="location" label="地区" size="mini">
                    <el-input v-model="registerForm.location" placeholder="选择地区"></el-input>
                </el-form-item>
                <el-form-item prop="introduction" label="简介" size="mini">
                    <el-input type="textarea" v-model="registerForm.introduction" placeholder="选择简介"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer">
                <el-button size="mini" @click="centerDialogVisible = false">取消</el-button>
                <el-button size="mini"  type="primary" @click="addSinger">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="修改歌手"  :visible.sync="deitVisible" width="400px" center>
            <el-form :model="form" ref="form" label-width="80px">
                <el-form-item prop="name" label="歌手名" size="mini">
                    <el-input v-model="form.name" placeholder="歌手名"></el-input>
                </el-form-item>
                <el-form-item prop="sex" label="性别" size="mini">
                    <el-radio-group v-model="form.sex">
                        <el-radio :label="0">女</el-radio>
                        <el-radio :label="1">男</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="birth" label="日期" size="mini">
                    <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item prop="location" label="地区" size="mini">
                    <el-input v-model="form.location" placeholder="选择地区"></el-input>
                </el-form-item>
                <el-form-item prop="introduction" label="简介" size="mini">
                    <el-input type="textarea" v-model="form.introduction" placeholder="选择简介"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer">
                <el-button size="mini" @click="centerDialogVisible = false">取消</el-button>
                <el-button size="mini" type="primary" @click="editSave">确定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="修改歌手"  :visible.sync="delVisible" width="400px" center>    
            <div align="center">删除不可恢复,是否确定删除?</div>
            <span slot="footer">
                <el-button size="mini" @click="delVisible = false">取消</el-button>
                <el-button size="mini" type="primary" @click="deleteRow">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>


<script>
    import {addSinger,getAllSinger,updateSinger,deleteSinger,deleteSingers} from '../api/index'
    export default {
        data () {
            return {
                centerDialogVisible : false,            // 显示是否显示新增弹出框
                deitVisible: false,                     // 修改歌手弹出是否显示
                delVisible: false,                      // 显示修改弹出框
                registerForm: {
                    name: '',
                    sex: '',
                    birth: '',
                    location: '',
                    introduction: ''
                },
                form: {                                 // 修改用户框

                },
                singers: [],                            
                tempSingers: [],
                select_word: '',
                pageSize:5,             // 每页的数据量
                currentPage:1,          // 当前为第几页
                idx: '',                // 当前选择项
                multipleSelection:[]    // 哪些项已经打钩了
            }
        },
        created () {
            this.getData()
        },
        methods: {
            // 添加歌手
            addSinger () {
                // 将时间转换为指定格式
                let d = this.registerForm.birth
                let date = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate()

                let params = new URLSearchParams()
                params.append('name',this.registerForm.name)
                params.append('sex',this.registerForm.sex)
                params.append('pic',"/images/singerPic/头像.jpg")
                params.append('birth',date)
                params.append('location',this.registerForm.location)
                params.append('introduction',this.registerForm.introduction)


                addSinger(params)
                    .then(res => {
                        if(res.code === 1) {
                            this.getData()
                            this.notify("添加成功","success")
                        }else {
                            this.notify("添加失败","error")
                        }
                    })
                    .catch(err => {
                        console.log(err)
                    })

                this.centerDialogVisible = false

            },

            // 查询所有歌手
            getData () {
                // 先请空
                this.singers = []

                // 查询
                getAllSinger()
                    .then(res => {
                        this.tempSingers = res
                        this.singers = res

                    })
            },
            // 上传图片
            uploadUrl (id) {
                return `${this.$store.state.HOST}/singer/updateSingerPic?id=${id}`
            },
            // 修改页数
            handleCurrentChange (val) {
                this.currentPage = val
            },

            // 弹出修改歌手框
            handleEdit(singer) {
                this.deitVisible = true

                // 实现数据回显
                this.form = {
                    id: singer.id,
                    name: singer.name,
                    sex: singer.sex,
                    birth: singer.birth,
                    location: singer.location,
                    introduction: singer.introduction
                }
            },

            // 提交修改信息
            editSave () {
                // 将时间转换为指定格式
                let d = this.form.birth
                let date = d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate()

                let params = new URLSearchParams()
                params.append('id',this.form.id)
                params.append('name',this.form.name)
                params.append('sex',this.form.sex)
                params.append('birth',date)
                params.append('location',this.form.location)
                params.append('introduction',this.form.introduction)


                updateSinger(params)
                    .then(res => {
                        if(res.code === 1) {
                            this.getData()
                            this.notify("修改成功","success")
                        }else {
                            this.notify("修改失败","error")
                        }
                    })
                    .catch(err => {
                        console.log(err)
                    })

                this.deitVisible = false
            },

            // 删除一名歌手
            deleteRow () {
                deleteSinger(this.idx)
                    .then(res => {
                        if(res.code === 1) {
                            this.notify(
                                "删除成功",
                                "success"
                            )
                            this.getData()
                        }else{
                            this.notify(
                                "删除失败",
                                "error"
                            )
                        }
                    })

                    this.delVisible = false
            },

            // 把已经选择的项赋值给multipleSelection
            handleSelectionChange (val) {
                this.multipleSelection = val
            },

            // 批量删除歌手
            delAll () {
                let singers = []
                this.multipleSelection.forEach((item,index) => {
                    singers[index] = item.id
                })
                deleteSingers(singers)
                    .then(res => {
                         if(res.code === 1) {
                            this.notify(
                                "删除成功",
                                "success"
                            )
                            this.getData()
                        }else{
                            this.notify(
                                "删除失败",
                                "error"
                            )
                        }
                    })
            },

            // 转向歌曲管理页面
            songEdit (id,name) {
                this.$router.push({
                    path: "/SongPage",
                    query: {
                        id,
                        name
                    }
                })
            }

        },
        computed: {
            data () {
                return this.singers.slice((this.currentPage-1)*this.pageSize,this.currentPage * this.pageSize)
            }
        },

        watch: {
            select_word (newValue,oldValue) {
                this.singers = this.tempSingers
                if(newValue !== '') {
                    this.singers = this.singers.filter((item,index) => {
                        return item.name.includes(newValue)
                    })
                }
            }
        }

    }
</script>


<style scoped lang="less">
    .singer-table{
        min-width: 800px;
        .singer-container{
            padding: 20px;
            border:1px solid #ddd;
            background-color: #fff;
            border-radius: 5px;
            .handle-box {
                margin-bottom: 20px;
                .handle-input{
                    width: 300px;
                    display: inline-block;
                }
            }
        }
        .singer-image{
            width: 100%;
            height: 80px;
            border-radius: 5px;
            margin-bottom: 5px;
            overflow: hidden;
            img{
                display: block;
                width: 100%;
                height: 100%;
            }
        }
        .singertables{
            width:100%;
            height:calc(100vh - 210px);
            overflow-y: scroll
        }
        .pagination{
            display: flex;
            height: 50px;
            justify-content: center;
            background-color: #fff;
            align-items: center;
        }
    }
</style>